<template>
  <!-- 项目选择 -->
  <a-select
    v-bind="$attrs"
    v-on="$listeners"
    @change="getProjectDeptId"
    :placeholder="placeholder"
    v-model="currentValue"
    allowClear
  >
    <a-icon v-if="fetching" style="color: #096dd9" slot="suffixIcon" type="loading" />
    <a-spin v-if="fetching" slot="notFoundContent" size="small" />
    <a-select-option v-else v-for="i in project" :key="i.projectId" :value="i.projectId">{{
      i.projectName
    }}</a-select-option>
  </a-select>
</template>
<script>
import { getAuditProject } from '@/api/themeChoose/themeChoose'

export default {
  name: 'SelectProject',
  inheritAttrs: false,
  props: {
    // v-model绑定的数据
    value: {
      type: [Array, String, Object, Number],
    },
    // 需要联动的表单名，
    form: {
      type: Object,
      default: function () {
        return {}
      },
    },
    // placeholder
    placeholder: {
      type: String,
      default: function () {
        return '请选择'
      },
    },
    // 需要联动的表单名
    keyValue: {
      type: String,
      default: function () {
        return 'branchCompanyId'
      },
    },
    ifMasterPlanAdjust: {
      type: Boolean,
      default() {
        return false
      },
    },
    //是否有项目停工
    ifProjectStopWork: {
      type: Boolean,
      default() {
        return false
      },
    },
    ifProjectSuspendedWork: {
      type: Boolean,
      default() {
        return false
      },
    },
    // 设计资料查a1 a2 a3类项目
    designTypeName: {
      type: Boolean,
      default() {
        return false
      },
    },
    // 设计变更查a1类项目
    ifDesignChange: {
      type: Boolean,
      default() {
        return false
      },
    },
    // 联动表单值改变时是否清空当前表单值
    isClear: {
      type: Boolean,
      default() {
        return true
      },
    },
    isInit: {
      type: Boolean,
      default() {
        return true
      },
    },
    // 自定义参数
    customQuery: {
      type: Object,
      default: null,
    },
    // 是否需要显示维保期的项目
    isMaintenance: {
      type: Boolean,
      default: () => false,
    },
    //如果是编辑，第一次不要重置绑定的值。。。。
    isEdit: {
      type: Boolean,
      default: () => false,
    },
  },
  mounted() {
    // 获取当前登录人选中岗位的组织层级
    this.orgLevel = JSON.parse(sessionStorage.getItem('ycloud-user_info'))?.currentPost?.code
    this.getAuditProjectList()
  },
  data() {
    return {
      isCanClear: !this.isEdit, // 是否清除
      fetching: false,
      project: [],
      querys: {},
      orgLevel: '', // 当前登录人选中岗位的组织层级
    }
  },
  methods: {
    getProjectDeptId(val) {
      this.getAuditProjectList()

      const target = this.project.find((item) => item.projectId === val)
      this.$listeners.projectDeptId && this.$emit('projectDeptId', target.projectDeptId)
      this.$listeners.projectNum && this.$emit('projectNum', target.projectNum)
      this.$listeners.pmIdHandler && this.$emit('pmIdHandler', target)

      this.$listeners.projects && this.$emit('projects', target ? target : {})
    },
    getAuditProjectList() {
      if (!this.attr) {
      // if (!this.attr && this.orgLevel != 'PD') {
        return
      }
      this.$nextTick(() => {
        this.fetching = true
        if (this.ifMasterPlanAdjust) {
          this.querys = { branchComIds: [this.attr], auditStatus: 2, ifMasterPlanAdjust: 0 }
        } else if (this.ifProjectStopWork) {
          this.querys = { branchComIds: [this.attr], auditStatus: 2, ifProjectStopWork: 0 }
        } else if (this.ifProjectSuspendedWork) {
          this.querys = { branchComIds: [this.attr], auditStatus: 2, ifProjectSuspendedWork: 0 }
        } else if (this.designTypeName) {
          this.querys = { branchComIds: [this.attr], auditStatus: 2, designTypeNames: ['a1类', 'a2类', 'a3类'] }
        } else if (this.ifDesignChange) {
          this.querys = { branchComIds: [this.attr], auditStatus: 2, designTypeNames: ['a1类'] }
        } 
        // else {
        //   this.querys = { branchComIds: [this.attr], auditStatus: 2 }
        // }
        if (this.customQuery) {
          this.querys = {
            ...this.querys,
            ...this.customQuery,
          }
        }
        // 当前人岗位组织为项目则不加分公司参数
        if (this.orgLevel != 'PD') {
          this.querys.branchComIds = [this.attr]
        } else {
          this.querys.branchComIds && delete this.querys.branchComIds
        }
        if (this.isMaintenance) this.querys.projectStatus = '1,2,3,4,5,6'
        getAuditProject(this.querys)
          .then((res) => {
            this.fetching = false
            if (res.code === 200) {
              this.project = res.data

              const target = this.project.find((item) => item.projectId === this.value)
              this.$listeners.projectDeptId && this.$emit('projectDeptId', target.projectDeptId)
            } else {
              this.$message.error('err:', res.message)
            }
          })
          .catch((res) => {
            this.fetching = false
          })
      })
    },
    // 获取指定项目的name
    getValueName(id) {
      const target = this.project.find((item) => item.projectId === id)
      return (target && target.projectName) || void 0
    },
  },
  computed: {
    // 需要联动的表单值
    attr() {
      // console.log(this.form[this.keyValue])
      return this.form[this.keyValue]
    },
    // 转换绑定的选中数据，与v-model同步
    currentValue: {
      get: function () {
        return this.value
      },
      set: function (value) {
        this.$emit('input', value)
      },
    },
  },
  watch: {
    // 监听联动表单值重置项目列表
    attr(val) {
      // if (this.orgLevel === 'PD') {
      //   return
      // }
      // 清空表单
      if (this.isClear) {
        this.currentValue = void 0
        this.$listeners.resetSelf && this.$emit('resetSelf') // 在搜索组件(SearchData)中重置自身的值
      }
      // 清空表单
      if (this.isEdit && this.isCanClear) {
        this.currentValue = void 0
        this.$emit('resetSelf') // 在搜索组件(SearchData)中重置自身的值
      } else {
        this.isCanClear = true
      }
      // 重置列表
      this.project = []
      val && this.getAuditProjectList()
    },
  },
}
</script>
